<template>
	<view>
    <!-- 搜索框 -->
		<view class="search">
      <uni-search-bar @input="input" :radius="100" cancelButton="none"></uni-search-bar>
    </view>
    <!-- 搜索建议列表 -->
    <view class="suggest-list" v-if="suggestList.length>0">
      <view class="suggest-item" v-for="(item,index) in suggestList" :key="index" @click="goToGoodsDetail(item.goods_id)">
        <view class="suggest-name">{{item.goods_name}}</view>
        <uni-icons type="arrowright" size="16"></uni-icons>
      </view>
    </view>
    
    <!-- 搜索历史列表 -->
    <view class="history" v-else>
      <!-- 标题部分 -->
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="17" @click="clearHistoryList"></uni-icons>
      </view>
      <view class="history-list">
        <uni-tag :text="item" v-for="(item,index) in historyList" :key="index" @click="goToGoodsList(item)"></uni-tag>
      </view>
    </view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
        // 定时器Id
				timerId:null,
        // 搜索词
        searchWord:'',
        // 搜索建议列表
        suggestList:[],
        // 搜索历史列表
        historyList:[]
			};
		},
    onLoad() {
      this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]') 
    },
    methods:{
      input(e) {
        clearTimeout(this.timerId)
        this.timerId = setTimeout(() => {
          this.searchWord = e.value
          // 发送请求，获取建议列表
          this.getSuggestList()
        console.log(e.value)
        },500)
      },
      async getSuggestList() {
        if(this.searchWord.trim().length===0) {
          this.suggestList = []
          return
        }
        const {data:res} = await uni.$http.get('/api/public/v1/goods/qsearch',{query:this.searchWord})
        if(res.meta.status !== 200) return uni.$showMsg()
        this.suggestList = res.message
        this.addHistoryList()
      },
      goToGoodsDetail(id) {
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?goods_id='+id
        })
      },
      // 添加搜索历史
      addHistoryList() {
        const index = this.historyList.findIndex(item => item===this.searchWord)
        // console.log(index,'-----')
        if(index>-1) {
          this.historyList.splice(index,1)
        }
        // console.log(this.historyList)
        this.historyList.unshift(this.searchWord)
        uni.setStorageSync('kw', JSON.stringify(this.historyList))
      },
      // 清空历史记录
      clearHistoryList() {
        this.historyList = []
        uni.setStorageSync('kw','[]')
      },
      // 跳转到商品列表页
      goToGoodsList(kw) {
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?query='+kw
        })
      }
    }
	}
</script>

<style lang="scss">
  // 实现吸顶效果
.search {
  position: sticky;
  top: 0;
  z-index: 999;
}
.suggest-list {
  padding: 0 8px;
  .suggest-item {
    font-size: 12px;
    padding: 8px 0;
    border-bottom: 1px solid #efefef;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .suggest-name {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      font-size: 14px;
      margin-right: 3px;
    }
  }
}
.history {
  padding: 0 5px;
  .history-title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 40px;
    font-size: 13px;
    border-bottom: 1px solid #efefef;
  }
  .history-list {
    display: flex;
    flex-wrap: wrap;
    .uni-tag {
    margin-top: 5px;
    margin-right: 5px;
    }
  }
}
</style>
